/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const cheerio = require('cheerio');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class ForbesCryptoBlockchain extends BaseJsonAdapter{

    initWinEnv(){
        global.window = {
            innerWidth:1232,
            innerHeight:632,
            screenX:11,
            screenY:22,
            screen:{
                width:1280,
                height:800
            }
        };

        global.document = {
            documentElement:{
                clientWidth:1217,
                clientHeight:550
            },
            body:{
                clientWidth:1217,
                clientHeight:1467
            },
            _cookie: [],

            get cookie(){
                return this._cookie;
            },

            set cookie(val){
                this._cookie.push(val);
            }
        };
    }

    async doRequest(){
        this.initWinEnv();
        return super.doRequest();
    }


    async reqJson(){
		const body = await this.doRequest();
        let decodeBody = iconv.decode(body, this.encoding).toString();
        let start = decodeBody.indexOf('fbs_settings.promoted_content =')+'fbs_settings.promoted_content ='.length
        let end = decodeBody.indexOf('fbs_settings.promoted_content = null;')-21;
        decodeBody = decodeBody.substring(start,end);
        let data = JSON.parse(decodeBody);
        let res={};
        res.data = [];
        for(let p in data){
            let contentPositions = data[p].contentPositions;
            console.log("--------contentPositions: " + JSON.stringify(contentPositions));
            if(contentPositions[0].date) {
                for(let i=0; i<contentPositions.length;i++) {
                    res.data.push(contentPositions[i])
                }

            }
        }
        console.log("-----decodeBody: " + decodeBody)
		return res;
	}
}
